import 'package:flutter/material.dart';
import 'package:untitled/services/world_time.dart';

class ChooseLocation extends StatefulWidget {
  const ChooseLocation({Key? key}) : super(key: key);

  @override
  State<ChooseLocation> createState() => _ChooseLocationState();
}

class _ChooseLocationState extends State<ChooseLocation> {
  
  List<WorldTime> locations = [
    WorldTime(url: 'Asia/Shanghai', location: "北京", flag: "china.png"),
    WorldTime(url: 'Europe/London', location: '伦敦', flag: 'uk.png'),
    WorldTime(url: 'Europe/Berlin', location: '雅典', flag: 'greece.png'),
    WorldTime(url: 'Africa/Cairo', location: '开罗', flag: 'egypt.png'),
    WorldTime(url: 'Africa/Nairobi', location: '内罗毕', flag: 'kenya.png'),
    WorldTime(url: 'America/Chicago', location: '芝加哥', flag: 'usa.png'),
    WorldTime(url: 'America/New_York', location: '纽约', flag: 'usa.png'),
    WorldTime(url: 'Asia/Seoul', location: '首尔', flag: 'south_korea.png'),
  ];
  


  void updateTime(index) async{
    WorldTime initState = locations[index];
    await initState.getData();
    print("initState= ${initState}");

    Navigator.pushNamed(context,"/home",arguments: {
      'location':initState.location,
      'flag':initState.flag,
      'time':initState.time,
      'isDayTime':initState.isDaytime
    });
  }
 
  // 生命周期函数
  @override
  void initState() {
    super.initState();
   
    //print("initState function ran");
  }
  @override
  Widget build(BuildContext context) {
   // print("build function ran");
    return Scaffold(
      backgroundColor: Colors.grey[200],
      appBar: AppBar(  // AppBar 拥有一个返回功能
        backgroundColor:Colors.blue[900],
        title: Text("选择一个国家"),
        centerTitle: true,
        elevation: 0,
      ),
      body:ListView.builder(
        itemCount: locations.length,
        itemBuilder: (context, index){
           return Padding(
             padding: EdgeInsets.symmetric(vertical: 1.0,horizontal: 4.0),
               child: ListTile(
                 onTap: (){
                   updateTime(index);
                 },
                 title: Text(locations[index].location as String),
                 leading: CircleAvatar(
                   backgroundImage: AssetImage('assets/${locations[index].flag}'),
                 ),
               ),
             ); 
        }
        
      )
    );
    
  }
  @override
  void dispose() {
    super.dispose();
    print("dispose function ran");
  }
}